﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebService.DAO;
using WebService.VO;

namespace WebService
{
    public partial class Test : System.Web.UI.Page
    {
        DataTable dt;
        DataTable dt1;
        RouteVO route;
        protected void Page_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            ItemDAO itemDAO = new ItemDAO();
            dt1 = itemDAO.getItemsList("vi");
            dt1.Columns.Add("Distance", typeof(Double));
        }

        protected void btn_FindRoute_Click(object sender, EventArgs e)
        {
            route = GDirections.parse(txt_Start.Text.ToString(), txt_End.Text.ToString());
            if (route != null)
            {
                StringBuilder temp = new StringBuilder();
                temp.Append("Route found\r");
                List<LatLng> points = route.Points;
                foreach (LatLng point in points)
                {
                    temp.Append(point.Latitude + "," + point.Longitude+"\n");
                }
                txt_Info.Text = temp.ToString();
                Session["Route"] = route;
                btn_FindPlaces.Visible = true;
            }
            else 
            {
                txt_Info.Text = "Route not found";
            }
        }

        protected void btn_FindPlaces_Click(object sender, EventArgs e)
        {
            if (Session["Route"] != null)
            {
                route = (RouteVO)Session["Route"];
            }
            if (route != null)
            {
                dt = GooglePlaces.getPlaces(dt1, route);
                gv_Places.DataSource = dt;
                gv_Places.DataBind();
            }
        }
    }
}